package jp.co.apl.haven_enquete.android.db;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

public class DatabaseHelperTest extends AndroidTestCase {

    @SmallTest
    public void testCreateTables() {
        DatabaseHelper helper = new DatabaseHelper(mContext);
        SQLiteDatabase db = helper.getWritableDatabase();

        try {

            Cursor cur = db.rawQuery("select * from answer", null);
            assertNotNull(cur);

            try {
                db.rawQuery("select * from zzz", null);
                fail();
            } catch (Exception e) {
                assertTrue(true);
            }

        } finally {
            db.close();
        }
    }

    @SmallTest
    public void testMatrixCursor() {
        MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c" });

        String dataA = "dataA";
        int dataB = 512;
        byte[] dataC = new byte[] { 3, 3, 3, 3, 3 };

        cursor.addRow(new Object[] { dataA, dataB, dataC });

        assertEquals(1, cursor.getCount());
        assertEquals(3, cursor.getColumnCount());
        assertTrue(cursor.moveToFirst());
        assertEquals("dataA", cursor.getString(0));
        assertEquals(512, cursor.getInt(1));
        try {
            cursor.getBlob(2);
            fail("BLOBが使えた");
        } catch (Exception e) {
            Log.i("haven", "BLOBは使えなかった");
        }
    }
}
